package com.anis.project.medcare.preferences;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import org.eclipse.jface.preference.*;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Button;
import org.eclipse.ui.IWorkbenchPreferencePage;
import org.eclipse.ui.IWorkbench;

import com.anis.jpa.medical.model.Common;
import com.anis.jpa.medical.model.Templates;
import com.anis.project.medcare.Activator;

import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;

/**
 * This class represents a preference page that is contributed to the
 * Preferences dialog. By subclassing <samp>FieldEditorPreferencePage</samp>, we
 * can use the field support built into JFace that allows us to create a page
 * that is small and knows how to save, restore and apply itself.
 * <p>
 * This page is used to modify preferences only. They are stored in the
 * preference store that belongs to the main plug-in class. That way,
 * preferences can be accessed directly via the preference store.
 */

public class ReportsTemplates extends FieldEditorPreferencePage implements
		IWorkbenchPreferencePage {

	private FieldEditor fieldEditor;

	public ReportsTemplates() {
		super(GRID);
		setPreferenceStore(Activator.getDefault().getPreferenceStore());
		setDescription("This is where to specify the Medical Report Location");

	}

	/**
	 * Creates the field editors. Field editors are abstractions of the common
	 * GUI blocks needed to manipulate various types of preferences. Each field
	 * editor knows how to save and restore itself.
	 */
	public void createFieldEditors() {
		fieldEditor = new FileFieldEditor(PreferenceConstants.MEDREP_PATH,
				"&Report Location:", getFieldEditorParent());
		addField(fieldEditor);

	}

	private byte[] transformFile(File file) throws IOException {
		byte[] buffer = new byte[(int) file.length()];
		InputStream ios = null;
		try {
			ios = new FileInputStream(file);
			if (ios.read(buffer) == -1) {
				throw new IOException(
						"EOF reached while trying to read the whole file");
			}
		} finally {
			try {
				if (ios != null)
					ios.close();
			} catch (IOException e) {
			}
		}

		return buffer;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.eclipse.ui.IWorkbenchPreferencePage#init(org.eclipse.ui.IWorkbench)
	 */
	public void init(IWorkbench workbench) {
	}

	@Override
	protected void performApply() {
		// TODO Auto-generated method stub
		super.performApply();
		fieldEditor.store();
		Templates template = new Templates();
		template.setTempname(Common.MEDREPTEMP);
		template.setMedicalOrdnanceTemplate(null);
		template.setRadioReportTemplate(null);
		File templateFile = new File(Activator.getDefault()
				.getPreferenceStore()
				.getString(PreferenceConstants.MEDREP_PATH));
		byte[] resultantFile;
		try {
			resultantFile = transformFile(templateFile);
			template.setMedicalReportTemplate(resultantFile);
			Activator.getDefault().databaseBinder().saveTemplate(template);
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
	}

}